import cv2, time
import numpy as np


def GetBackGround():
    cap = cv2.VideoCapture(0)
    ret = cap.set(3, 1920)
    ret = cap.set(4, 1080)
    count = 0
    while (1):
        ret, frame = cap.read()

        sframe = cv2.resize(frame,(1920,1080))
        cv2.imshow('origin', sframe)  # 显示原始视频

        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break
        if k == 97:
            cv2.imwrite('./{}.jpg'.format(count),frame)
            count += 1
            print('capture')

    cap.release()
    cv2.destroyAllWindows()


def getArea(image, x, y, w, h):
    """
    获取图片的一个区域的图形 ,返回面积(像素值)
    image 是一个二值图，信息点是白色
    """
    zone = image[y:y + h, x:x + w]
    area = 0

    area = np.sum(zone == 255)  # 等价于后面的循环
    # for row in zone:
    #     for col in row:
    #         if col == 255:
    #             area += 1
    # print("area: ", area)
    return area


if __name__ == '__main__':
    GetBackGround()
